#include <iostream>
#include <stdio.h>
#include <list>
#include <vector>

using namespace std;

void test_list1()
{
    list<int> lt1;
    list<int> lt2(4, 250);
    vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
    list<int> lt3(v.begin() + 1, v.end() - 1);
    list<int> lt4(v.rbegin() + 1, v.rend() - 1);
    list<int> lt5(lt2);

    for(int e : lt1)
    {
        cout << e << " ";
    }
    cout << endl;

    for(int e : lt2)
    {
        cout << e << " ";
    }
    cout << endl;
    
    for(int e : lt3)
    {
        cout << e << " ";
    }
    cout << endl;
    for(int e : lt4)
    {
        cout << e << " ";
    }
    cout << endl;

    list<int>::iterator it = lt5.begin();
    while(it != lt5.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
}

//1.begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动
//2.rbegin与rend为反向迭代器,都迭代器执行++操作,迭代器向前移动
void test_list2()
{
    int array[] = {1,2,3,4,5,6,7,8,9,0};
    list<int> lt(array, array + sizeof(array) / sizeof(array[0]));

    //使用正向迭代器
    for(list<int>::iterator it = lt.begin(); it != lt.end(); ++it)
    {
        cout <<  *it << " ";
    }
    cout << endl;

    //使用反向迭代器
    for(list<int>::reverse_iterator rit = lt.rbegin();
        rit != lt.rend(); ++rit)
    {
        cout << *rit << " ";
    }
    cout << endl;
    
    //void print_list(const list<int>& l);

    //print_list(lt);

    
}

//void print_list(const list<int>& l)
//{
//    for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
//    {
//        //这里调用的是list的begin() const, 返回list的const_iterator对象
//        *it = 1; //编译不通过
//    }
//    cout << endl;
//}

void test_list3()
{
    int array[] = {1,2,3,4,5,6,7};
    list<int> lt1;
    cout << lt1.empty() << endl;
    cout << lt1.size() << endl;
    list<int> lt2(array, array + sizeof(array) / sizeof(array[0]));
    cout << lt2.empty() << endl;
    cout << lt2.size() << endl;

}

void test_list4()
{
    int array[] = {2,3,4,5,6,7,8};
    list<int> lt(array, array + sizeof(array) / sizeof(array[0]));

    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;

    lt.front() = 99;
    lt.back() = 100;
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;

}

void test_list5()
{
    //push_front pop_front push_back pop_back
    cout << "push_front pop_front push_back pop_back" << endl;
    int array[] = {2,3,4,5,6,7,8};
    list<int> lt(array, array + sizeof(array) / sizeof(array[0]));
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;

    lt.push_front(1);
    lt.push_back(9);
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;

    lt.pop_front();
    lt.pop_back();
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;

    //insert
    cout << "insert" << endl;
    list<int>::iterator it = lt.begin();
    it++;
    it++;
    it = lt.insert(it, 250);
    it = lt.begin();
    lt.insert(it, 5, 100);
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;

    list<int>::iterator it1 = lt.begin();
    it1++;
    it1++;
    list<int>::iterator it2 = lt.end();
    it2--;
    it2--;
    lt.insert(it1, it2, lt.end());
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;

    //erase
    cout << "erase" << endl;
    it2 = lt.erase(it2);
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;
    
    while(*it2 != 4)
    {
        --it2;
    }
    it2 = lt.erase(it2, lt.end());
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;

    //swap
    cout << "swap" << endl;
    list<int> lt1(2, 250);
    cout << "lt1: ";
    for(int& e : lt1)
    {
        cout << e << " ";
    }
    cout <<endl;
    cout << "lt: ";
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;
    lt.swap(lt1);
    cout << "lt: ";
    for(int& e : lt)
    {
        cout << e << " ";
    }
    cout <<endl;
    cout << "lt1: ";
    for(int& e : lt1)
    {
        cout << e << " ";
    }
    cout <<endl;
    
    //clear
    cout << "clear" << endl;
    lt1.clear();
    cout << "lt1: ";
    for(int& e : lt1)
    {
        cout << e << " ";
    }
    cout <<endl;

    //resize
    cout << "resize" << endl;
    for(int i = 1; i < 10; ++i)
    {
        lt1.push_back(i);
    }
    lt1.resize(5);
    lt1.resize(8, 100);
    lt1.resize(12);

    cout << "lt1 contains: ";
    for (list<int>::iterator it = lt1.begin(); it != lt1.end(); ++it)
    {
        cout << *it << ' ';
    }
    cout<< endl;
}

void test_list6()
{
    //reverse
    cout << "reverse" << endl;
    int array[] = {9,7,8,6,5,6,3,4,4,2,1};
    list<int> lt1(array, array + sizeof(array) / sizeof(array[0]));
    lt1.reverse();
    for(int& e : lt1)
    {
        cout << e << " ";
    }
    cout << endl;

    //sort
    cout << "sort" << endl;
    lt1.sort();
    for(int& e : lt1)
    {
        cout << e << " ";
    }
    cout << endl;

    //unique(去重)
    //使用这个函数之前一定要进行排序
    cout << "unique" << endl;
    lt1.unique();
    for(int& e : lt1)
    {
        cout << e << " ";
    }
    cout << endl;

    //remove
    cout << "remove" << endl;
    int array2[] = {23, 76, 99, 99, 16, 59, 33, 33, 33, 100};
    list<int> lt2(array2, array2 + sizeof(array2) / sizeof(array2[0]));
    lt2.remove(99);
    lt2.remove(33);
    for(int& e : lt2)
    {
        cout << e << " ";
    }
    cout << endl;
}

int main()
{
    
    //构造函数
    //test_list1();

    //iterator
    //test_list2();

    //capacity
    //test_list3();
    
    //element_access
    //test_list4();

    //modifiers
    //test_list5();
    
    //operations
    test_list6();

    return 0;
}
